/*************************
Diese Software ist ein Beispiel (sample code) und unterliegt der Microsoft Public License. 
Die Verwendung des Codes ist unter den Bedingungen der Microsoft Public License erlaubt.
*************************
This software is sample code and is subject to the Microsoft Public License. 
You may use this code according to the conditions of the Microsoft Public License.
*************************/
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Web;
using Egora.Stammportal.HttpReverseProxy.Mapping;
using NUnit.Framework;

namespace Egora.Stammportal.HttpReverseProxy.UnitTests
{
  [TestFixture]
  public class HeaderTransformerTest
  {
    [Test]
    [Ignore] // test no longer works since Framwork 3.0 installed
    public void RightSideHeader_FromCustomLeftSideHeader()
    {
      PathMap map =
        PathMap.CreateFromFile(
          @"MappingTest\Mapping.xml");
      RemoteApplication.CreateRemoteApplications(map);

      HttpContext context =
        HttpContextHelper.CreateHttpContext("GET", "/localtest/TestPage.aspx/someinfo", "name1=value1");

      RemoteApplication remoteApplication = RemoteApplication.GetRemoteApplication(context.Request);
      Assert.IsNotNull(remoteApplication, "remoteApplication");

      NameValueCollection headers = new NameValueCollection();
      headers.Add("X-Custom", "CustomValue");
      headers.Add("Range", "messages=1-20,25-30");
      HttpRequestHelper.AddHeaders(context.Request, headers);
      using (Stream inputBuffer = RequestHandler.GetInputStream(context.Request))
      {
        HttpWebRequest webRequest = remoteApplication.CreateRightSideRequest(context.Request, inputBuffer);
        Assert.IsNotNull(webRequest, "Request is null.");
        Assert.AreEqual("GET", webRequest.Method);
        Assert.IsNotNull(webRequest.Headers["X-Custom"], "CustomHeader");
        Assert.AreEqual("CustomValue", webRequest.Headers["X-Custom"]);
        Assert.IsNotNull(webRequest.Headers["Range"], "Range");
        Assert.AreEqual("messages=1-20,25-30", webRequest.Headers["Range"]);
      }
    }

    [Test]
    public void LocationHeaderTest()
    {
      string location =
        "http://localhost:9090/fkb2local/views/client.api.DatenImportierenLandesIT.jsf?FT=X";

      PathTransformer transformer = new PathTransformer("http://localhost:9090/fkb2local/", "/bmi.gv.at/fk2web-t/");

      Assert.AreEqual("/bmi.gv.at/fk2web-t/views/client.api.DatenImportierenLandesIT.jsf?FT=X", transformer.AdjustPath(location));
    }

    [Test]
    public void LocationHeaderTest2()
    {
      string location =
        "http://localhost:9090/fkb2local/views/client.api.DatenImportierenLandesIT.jsf?FT=X";
      string expectedLaoction = location;

      PathTransformer transformer = new PathTransformer("https://pvawp.bmi.gv.at/bmi.gv.at/fk2web-t/", "/bmi.gv.at/fk2web-t/blabla.jsp");

      Assert.AreEqual(expectedLaoction, transformer.AdjustPath(location));
    }

    [Test]
    [Ignore]
    public void RangeTest()
    {
      HttpRequest leftRequest = new HttpRequest("dummy", "http://somewhere.com", "dummy");
      leftRequest.Headers["range"] = "bytes=300-";
      HttpWebRequest rightRequest = (HttpWebRequest) WebRequest.Create("http://nowhere.com/");
      HeaderTransformer transformer = new HeaderTransformer(leftRequest, rightRequest, PvpTokenHandling.chain, "dummy");
      transformer.Transform();
      Assert.AreEqual("bytes=300-", rightRequest.Headers["range"]);
    }
  }
}